Welcome back.
Sometimes when we're making games in Roblox, we would like to be able to listen for when players or
parts enter specific zones.
Or we would like to be able to check for any players or parts in a specific region.
In our game, there are Roblox API functions that exist for these purposes, but they can be difficult
to understand or set up on our own.
That's why another developer created a module script that allows us to easily set up zones in our game,
where we can listen for when players or parts enter them, or check for any parts within that zone.
This module is called Zone Plus, and I've attached this dev forum post to the lectures so you can go
ahead and grab the module yourself to start using it in your games.
If we take a look at the documentation for Zone Plus, there are a lot of events, functions and properties
that we can use to set up zones in our game.
One example application of Zone Plus is going to be for a sliding door in our game, where we would
like it to open automatically when a player gets nearby and then close automatically when there are
no players near it.
This is our model right here.
And I've tagged it with the tag of sliding door.
And it's a very basic door system where we just have a part that is going to be our door.
We have this detect part, which is going to be the zone.
We're going to check for players.
And then we have another small part where this is going to be the position.
We're going to set the door when we want to open, and then it'll close and go back to its original
state when there are no players nearby.
I've created this very simple script inside of server script service, where it just goes through and
gets all of the models in our game that have this particular tag of sliding door.
And then we use a class to create an object for that door.
If we go ahead and hop into this class, it's a very basic class where we just pass the model, it sets
up the self table for it, and then we also have a cleanup function to destroy the object when the door
itself is destroyed and stuff like that.
However, now we want to set up this class to go ahead and listen for when players enter into the zone
of our door.
Leave the zone of our door and we want to open and close that door.
So let's go ahead and first get the module script for Zone Plus.
I've placed it as a child of the sliding door class down here.
This is my zone module script and I'll just call it zone up here.
And we'll require script dot zone.
And then I also have my better tween system that we've created earlier.
If you remember in our previous project to allow us to create tweens on the server and then have those
tweens played on the client.
So that way it looks very smooth.
So I'm going to get that as well.
Better tween and will require it from script dot better tween.
Now when our constructor gets called, we'll go ahead and add some properties to our table.
For example, we'll create a reference to the model itself.
We'll create a reference to the door which is going to be model dot door.
And then we're also going to get a reference to that zone.
But we want to go ahead and create a zone object from our zone class.
So I'm going to call this property uh detect zone.
And it's going to be equal to zone.
And then we use the dot new constructor to create a new zone.
And what we pass here is going to be an object or multiple objects that are going to be acting as the
parts for this zone.
In that case, it's going to be my detect part right here in my door model.
So we can go ahead and pass model dot detect part.
And now we're going to get a zone object that has all of those events, functions and properties that
we can go ahead and listen to.
Now.
I also want to be able to keep track of when my door is opened or closed.
So I'll create a boolean, I'll call it is open and we're going to set it to false by default because
all of our doors are currently closed.
And then I want to create another two properties.
One is going to store the open sea frame and the close sea frame for the door.
So we'll call the first one close sea frame.
And that's just equal to our self dot door dot sea frame.
And then for the open sea frame that's going to be equal to our self dot model dot open position that
part.
And then we're going to get that part sea frame.
Now we can go ahead and listen to two events within our detect zone object.
The first event in our detect zone object is called a player entered.
And we can go ahead and connect a function to this.
This event is going to fire any time a player enters into our zone.
So when a player enters into our zone, we would like to open the door.
So let's go ahead and create two functions that are going to belong to the meta table.
One of these functions is going to be for opening the door, and then we'll have another function for
closing the door.
When we want to open the door, we first want to check whether or not the door is open.
So if our door is already opened, then we're just going to return because we don't care.
Otherwise we're going to set is open equal to true.
And then we can use our better tween module and create a new tween.
And that's going to be on self dot door.
We'll do a new tween info.
We'll tween it for like 0.5 seconds.
And then we'll do an enemy's style of sign.
And then we can go ahead and change the key frame of our door to be equal to that self dot opened key
frame.
Then we can basically just copy this and do the exact same thing for our close function.
But this time we want to check if our door is already closed.
So if it's not open then we can just go ahead and return.
Otherwise we'll set is open equal to false.
And this time we want to set the key frame equal to the closed key frame.
Now, what we can go ahead and do is we can listen for when a player enters that zone around our door.
And if a player does enter that zone around our door, we want to go ahead and open the door, right.
So now let's go ahead and test this out and see if our door opens when we get close to it.
So if I go and select my zone here, as soon as my player's character gets into this zone, my door
should open and there we go.
That event fired and now the door has opened.
Now what we need to do is we need to go ahead and close the door.
When there are no players within this zone or when my player has exited the zone.
And that's what we're going to go ahead and use another event in the detect zone, which is called player
exited.
And we can go ahead and connect a function to this.
Now when a player exits the zone we want to go ahead and close the door.
But we only want to close it if there are no other players within that zone around my door.
Thankfully, our zone object actually has a function where we can go ahead and grab all of the players
that are inside of our zone.
So if we refer to our detect zone, there's a function called Get Players, and this is going to return
an array of all the players that are currently in our zone.
So we can store that in a variable called players.
And we can check if the number of players, or if the number of objects in this array is greater than
or equal to one, meaning there are players in the zone.
If there are, then we're just going to return because we don't want to close the door yet.
Otherwise, if there are no players in the zone, then we can go ahead and just close the door.
If we go and walk over and touch the zone.
As you can see, the door opens and if we exit the zone.
Unfortunately, it's not closing yet.
So let's go ahead and see what we did wrong.
And the reason it's not working is because we accidentally forgot to include a C and our C frame for
the closed C frame.
So fix that.
Let's go ahead and try this out again.
If we go and walk up to our door, as you can see, it opens.
And then once we exit the zone, there we go.
Our door closes.
Very cool.
Now if we have multiple players in our game, if my player approaches the door, as you can see, it
opens.
And then if my other player approaches the door, nothing happens.
But as soon as this player leaves, as you can see, our door still remains open because my player two
is still within that zone.
But once this player leaves as well, now the door closes.
So now we have a very cool automatic sliding door that will only close once players have left the zone
that's nearby to the door, and then it automatically opens once a player enters into that zone.
Of course, the amount of possibilities with Zone Plus is absolutely endless, and you can go ahead
and look through all of the different events for our zones.
You can look at the different properties, as well as the different functions, to allow you to create
basically anything you want.
With these zones, such as ambiance zones, you can listen for maybe one specific NPCs, enter a zone
to hurt them, and a lot more things can be done with this module.
So let's go ahead and take one last quick overview of the documentation.
As you can see with the constructor we pass a container and it says a container is used to define the
boundaries of a zone.
It can be any non base part instance such as a model folder etc. that contains descendant base parts.
Alternatively, a container can be a singular base part instance or a table containing an array of base
parts.
So you can really pass a lot of different things.
Here.
You can pass a folder, a model, an array, or just a part.
We also have the ability to find just the local player within a zone.
If you want to set up Ambiance zone, this is going to be really useful.
This function, as well as the local player entered and the local player exited events.
As you can see, they are marked as client only.
Of course, those other two events we just used player entered and player exited.
They execute once a player enters or exits the zone as well as they pass the player instance that has
entered or exited the zone.
We can also check to see if a part has entered our zone.
We can check when a part has exited, and we can also check to see whether an item has entered or exited
the zone.
What is a item?
Well, an item is a particular thing in your game that you want to track, and we do so by using the
function track item and it says right here.
This is used to detect your own custom instances within zones such as NPCs, and is recommended replacement
for part events.
Slash methods.
An item can be any base part or an NPC that has a humanoid and a humanoid part.
Once they are tracked, it can be listened for with the zone item entered and item exited events.
So if there are some specific things that you want to listen for when exiting or entering a zone, then
you would use this function to track those items.
And if those items enter or exit your zone, then the item entered or the item exited events are going
to fire.
Very cool.
That's all I have for you in this lecture.
I hope you enjoyed and I'll see you in the next one.